import java.sql.*;
import java.util.*;
import java.util.Date;

public class Main {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://150.158.80.216/3chengjipaixu";
    static final String USER = "3chengjipaixu";
    static final String PASS = "3chengjipaixu";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement();
            boolean flag = true;
            while (flag) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入需要做的事：1:录入成绩 2:排序 3:退出");

                String menu = scanner.next();
                if ("1".equals(menu)) {


                    String sql;
                    System.out.println("请输入stu_no：");
                    String stuNo = scanner.next();
                    System.out.println("请输入科目：");
                    String kemu = scanner.next();
                    System.out.println("请输入成绩：");
                    int score = scanner.nextInt();

                    sql = "INSERT INTO score ( stu_no, kemu, total) " +
                            "VALUES ('" + stuNo + "', '" + kemu + "'," + score + ")";
                    stmt.executeUpdate(sql);

                } else if ("2".equals(menu)) {
                    List<Student> students = new ArrayList<>();
                    String sqlRead = "SELECT * FROM score";
                    ResultSet rs = stmt.executeQuery(sqlRead);
                    while (rs.next()) {
                        String stuNo = rs.getString("stu_no");
                        String kemu = rs.getString("kemu");
                        int score = rs.getInt("total");

                        Student student = new Student();
                        student.setStuNo(stuNo);
                        student.setKemu(kemu);
                        student.setScore(score);
                        students.add(student);
                    }


                    System.out.println("使用Collections.sort进行排序");
                    long startTime = System.nanoTime();

                    Collections.sort(students, new Comparator<Student>() {
                        public int compare(Student s1, Student s2) {
                            return s2.getScore() - s1.getScore();
                        }
                    });

                    for (Student student : students) {
                        System.out.println(student.getStuNo() + ": " + student.getScore());
                    }
                    long endTime = System.nanoTime();
                    long duration = (endTime - startTime);
                    System.out.println("耗时 : " + duration / 1000 + "毫秒");


                    startTime = System.nanoTime();
                    Student[] studentsA = new Student[students.size()];
                    for (int i = 0; i < students.size(); i++) {
                        studentsA[i] = students.get(i);
                    }


                    Arrays.sort(studentsA, new Comparator<Student>() {
                        public int compare(Student s1, Student s2) {
                            return s2.getScore() - s1.getScore();
                        }
                    });
                    for (Student student : studentsA) {
                        System.out.println(student.getStuNo() + ": " + student.getScore());
                    }
                    endTime = System.nanoTime();
                    duration = (endTime - startTime);
                    System.out.println("耗时 : " + duration / 1000 + "毫秒");


                    startTime = System.nanoTime();
                    Student[] studentsAA = new Student[students.size()];
                    for (int i = 0; i < students.size(); i++) {
                        studentsAA[i] = students.get(i);
                    }
                    for (int i = 1; i < studentsAA.length; i++) {
                        int j = i;
                        Student temp = studentsAA[i];
                        while (j > 0 && temp.getScore() < studentsAA[j - 1].getScore()) {
                            studentsAA[j] = studentsAA[j - 1];
                            j--;
                        }
                        studentsAA[j] = temp;
                    }
                    for (Student student : studentsAA) {
                        System.out.println(student.getStuNo() + ": " + student.getScore());
                    }
                    endTime = System.nanoTime();
                    duration = (endTime - startTime);
                    System.out.println("插入排序耗时 : " + duration / 1000 + "毫秒");


                    startTime = System.nanoTime();
                    Student[] studentsM = new Student[students.size()];
                    for (int i = 0; i < students.size(); i++) {
                        studentsM[i] = students.get(i);
                    }
                    for (int i = 0; i < studentsM.length - 1; i++) {
                        for (int j = 0; j < studentsM.length - 1 - i; j++) {
                            if (studentsM[j].getScore() > studentsM[j + 1].getScore()) {
                                Student temp = studentsM[j];
                                studentsM[j] = studentsM[j + 1];
                                studentsM[j + 1] = temp;
                            }
                        }
                    }
                    for (Student student : studentsM) {
                        System.out.println(student.getStuNo() + ": " + student.getScore());
                    }
                    endTime = System.nanoTime();
                    duration = (endTime - startTime);
                    System.out.println("冒泡排序耗时 : " + duration / 1000 + "毫秒");


                    startTime = System.nanoTime();
                    Student[] studentsX = new Student[students.size()];
                    for (int i = 0; i < students.size(); i++) {
                        studentsX[i] = students.get(i);
                    }
                    for (int i = 0; i < studentsX.length - 1; i++) {
                        int minIndex = i;
                        for (int j = i + 1; j < studentsX.length; j++) {
                            if (studentsX[j].getScore() < studentsX[minIndex].getScore()) {
                                minIndex = j;
                            }
                        }
                        Student temp = studentsX[i];
                        studentsX[i] = studentsX[minIndex];
                        studentsX[minIndex] = temp;
                    }
                    for (Student student : studentsX) {
                        System.out.println(student.getStuNo() + ": " + student.getScore());
                    }
                    endTime = System.nanoTime();
                    duration = (endTime - startTime);
                    System.out.println("选择排序耗时 : " + duration / 1000 + "毫秒");


                    System.out.println("排序完毕!");


                } else if ("3".equals(menu)) {
                    System.out.println("Goodbye!");
                    flag = false;
                    break;
                } else {
                    System.out.println("请正确输入！");
                }
            }

            stmt.close();
            conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }


    }
}

